<?php
namespace Admin\Controller;
use think\controller;
class NavsController extends AuthController{
    public function index(){

        $this->sessions=M('blog_admin')->where(array('aid'=>$_SESSION['name']))->find();
        $this->page_title="تىزىملىكلەر";
        $this->navs=M('navs')->order('sort desc')->select();
        $this->display();

    }

    public function add()
    {
        $this->page_title="تىزىملىك قوشۇش";
        $this->display();
    }

    //提交
    public function addhend()
    {
        //数据验证
        $rules=[
            ['name','require','菜单名称必须填！'],
            ['name','','菜单名称不能重复！',0,'unique',1],
            ['url','require','菜单连接地址必须填！'],
        ];
        if(!M('navs')->validate($rules)->create()) $this->error(M('navs')->getError());
        if(!M('navs')->add(I('post.'))) $this->error('添加失败！');
        $this->success('添加成功',U('Navs/index'),1);
    }

    //删除菜单
    public function deleteNavs($id)
    {
        if(M('navs')->where(array('id'=>$id))->delete()){
            $data=['status'=>1,'message'=>'ئۆچۈرۈش تامام'];
        }
        else{
            $data=['status'=>1,'message'=>'ئۆچۈرۈش تامام'];
        }
        $this->ajaxReturn($data);
    }

    //编辑菜单
    public function edit($id)
    {
        $this->page_title="تىزىملىك تەھرىرلەش";
        $this->navs=M('navs')->where(array('id'=>$id))->find();
        $this->display();
    }
    //提交编辑后的表单
    public function savehend()
    {
       if(!M('navs')->save(I('post.'))) $this->error('菜单编辑失败！');
       $this->success('菜单编辑成功',U('Navs/index'),1);
    }

    //菜单排序
    public function navsSort()
    {
        if(M('navs')->where(array('id'=>I('post.id')))->save(I('post.'))) {
            $data=['status'=>1,'message'=>'تەرتىبلەش تامام'];
        }
        else{
            $data=['status'=>0,'message'=>'تەرتىبلەش مەغلۇب بولدى!'];
        }
        $this->ajaxReturn($data);
    }
}